/*
 * Helstius--Basic chemistry application
 * This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 * Copyright 2012 Jaked122
 */
package org.helstius.Gui;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;

import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.border.CompoundBorder;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.border.EtchedBorder;

import org.helstius.chem.ChemicalEquation;
import org.helstius.chem.Parser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.TreeMap;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class EquationValidation extends JPanel {
	private JTextField FormulaField;
	JTextPane txtpnElementCounts;
	HashMap<String, Integer> g;
	private JSpinner Coeff;

	public int getCoefficient() {
		return ((Integer) Coeff.getValue()).intValue();
	}

	/**
	 * Set the Equation's coefficient
	 * 
	 * @param x
	 */
	public void setCoefficient(int x) {
		Coeff.setValue(new Integer(x));
	}

	ChemicalEquation result;

	public HashMap<String, Integer> calculatecount() {
		if (change) {
			result = new ChemicalEquation(FormulaField.getText());
			change = false;
		}
		// StringBuilder c = new StringBuilder();
		// try {
		//
		// g = t.getcounts(FormulaField.getText());
		//
		// } catch (NoSuchElementException a) {
		// ErrorMessageDialog aa = new ErrorMessageDialog(
		// "You have forgotten a coefficient, or you have forgotten that element symbols must be capitalized");
		// return new HashMap<String, Integer>();
		// }
		// Parser.multiplyMaps(g, ((Integer) Coeff.getValue()).intValue());
		// for (String a : g.keySet()) {
		// c.append(a + ":" + (g.get(a)) + "\n");
		//
		// }

		// txtpnElementCounts.setText(c.toString());
		g = result.getElementCounts(getCoefficient());
		return g;
	}

	boolean change = false;

	/**
	 * Create the panel.
	 */
	public EquationValidation() {
		setBorder(new CompoundBorder(new MatteBorder(1, 1, 1, 1,
				(Color) new Color(0, 0, 0)), new EtchedBorder(
				EtchedBorder.LOWERED, null, new Color(255, 0, 0))));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JLabel lblFormula = new JLabel("Formula");
		GridBagConstraints gbc_lblFormula = new GridBagConstraints();
		gbc_lblFormula.fill = GridBagConstraints.BOTH;
		gbc_lblFormula.gridheight = 2;
		gbc_lblFormula.insets = new Insets(0, 0, 5, 5);
		gbc_lblFormula.anchor = GridBagConstraints.EAST;
		gbc_lblFormula.gridx = 0;
		gbc_lblFormula.gridy = 0;
		add(lblFormula, gbc_lblFormula);

		FormulaField = new JTextField();
		FormulaField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				change = true;
			}
		});

		GridBagConstraints gbc_FormulaField = new GridBagConstraints();
		gbc_FormulaField.gridheight = 2;
		gbc_FormulaField.insets = new Insets(0, 0, 5, 0);
		gbc_FormulaField.fill = GridBagConstraints.BOTH;
		gbc_FormulaField.gridx = 1;
		gbc_FormulaField.gridy = 0;
		add(FormulaField, gbc_FormulaField);
		FormulaField.setColumns(10);

		JButton btnParseFormula = new JButton("Parse Formula");
		btnParseFormula.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				calculatecount();
			}
		});
		GridBagConstraints gbc_btnParseFormula = new GridBagConstraints();
		gbc_btnParseFormula.fill = GridBagConstraints.BOTH;
		gbc_btnParseFormula.gridwidth = 2;
		gbc_btnParseFormula.insets = new Insets(0, 0, 5, 0);
		gbc_btnParseFormula.gridx = 0;
		gbc_btnParseFormula.gridy = 2;
		add(btnParseFormula, gbc_btnParseFormula);

		txtpnElementCounts = new JTextPane();
		txtpnElementCounts.setFont(new Font("Tahoma", Font.PLAIN, 11));
		txtpnElementCounts.setText("Element Counts:");
		GridBagConstraints gbc_txtpnElementCounts = new GridBagConstraints();
		gbc_txtpnElementCounts.gridheight = 3;
		gbc_txtpnElementCounts.fill = GridBagConstraints.BOTH;
		gbc_txtpnElementCounts.gridx = 1;
		gbc_txtpnElementCounts.gridy = 3;
		add(txtpnElementCounts, gbc_txtpnElementCounts);

		Coeff = new JSpinner();
		Coeff.setModel(new SpinnerNumberModel(new Integer(1), null, null,
				new Integer(1)));
		Coeff.setToolTipText("The number of Moles given\r\n(Multiply the number of each element by this)");
		GridBagConstraints gbc_Coeff = new GridBagConstraints();
		gbc_Coeff.insets = new Insets(0, 0, 5, 5);
		gbc_Coeff.gridx = 0;
		gbc_Coeff.gridy = 4;
		add(Coeff, gbc_Coeff);

	}

}
